package com.asset.dispoal.business.bsAgreement.service.commom;

import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.bsAgreement.dto.AgreementDataRequest;
import com.asset.dispoal.business.bsAgreement.dto.AgreementDataResponse;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/5/11$
 */
@Component
public class CaseTrackingCommand implements BaseCommand<AgreementDataRequest, Result> {

    @Autowired
    AgreementMapper agreementMapper;

    @Override
    public Result execute(AgreementDataRequest agreementDataRequest) {
        String permission = (String) GlobalHeaderThreadLocal.get().getExtraMap().get("permission");
        agreementDataRequest.setPermission(permission);
        int count = agreementMapper.queryAgreementsCount(agreementDataRequest);
        Page<AgreementDataRequest> page = PagingQuery.getPage(count, agreementDataRequest);
        PageVO<AgreementDataResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<AgreementDataResponse> agreementDataResponses = agreementMapper.queryAgreementDatas(page);
            pageVO.setResult(agreementDataResponses);
        }
        return Result.ok(pageVO);
    }
}
